<?php

class Release {

    private $_id;
    private $_images;
    private $_artists;
    private $_title;
    private $_extraartists;
    private $_formats;
    private $_genres;
    private $_styles;
    private $_country;
    private $_released;
    private $_tracklist;

    public function getId() {
        return $this->_id;
    }

    public function setId($id) {
        $this->_id = $id;
        return $this;
    }

    public function getImages() {
        return $this->_images;
    }

    public function setImages($images) {
        $this->_images = $images;
        return $this;
    }

    public function getArtists() {
        return $this->_artists;
    }

    public function setArtists($artists) {
        $this->_artists = $artists;
        return $this;
    }

    public function getTitle() {
        return $this->_title;
    }

    public function setTitle($title) {
        $this->_title = $title;
        return $this;
    }

    public function getExtraartists() {
        return $this->_extraartists;
    }

    public function setExtraartists($extraartists) {
        $this->_extraartists = $extraartists;
        return $this;
    }

    public function getFormats() {
        return $this->_formats;
    }

    public function setFormats($formats) {
        $this->_formats = $formats;
        return $this;
    }

    public function getGenres() {
        return $this->_genres;
    }

    public function setGenres($genres) {
        $this->_genres = $genres;
        return $this;
    }

    public function getStyles() {
        return $this->_styles;
    }

    public function setStyles($styles) {
        $this->_styles = $styles;
        return $this;
    }

    public function getCountry() {
        return $this->_country;
    }

    public function setCountry($country) {
        $this->_country = $country;
        return $this;
    }

    public function getReleased() {
        return $this->_released;
    }

    public function setReleased($released) {
        $this->_released = $released;
        return $this;
    }

    public function getTracklist() {
        return $this->_tracklist;
    }

    public function setTracklist($tracklist) {
        $this->_tracklist = $tracklist;
        return $this;
    }

    function __construct(array $donnees) {
        $donnees['id'] = $donnees['__attributes__']['id'];
        if (isset($donnees['title'][0]['__content__'])) {
            $donnees['title'] = $donnees['title'][0]['__content__'];
        } else {
            $donnees['title'] = $donnees['title'][0];
        }
        if (isset($donnees['released'])) {
            if (isset($donnees['released'][0]['__content__'])) {
                $donnees['released'] = $donnees['released'][0]['__content__'];
            } else {
                $donnees['released'] = $donnees['released'][0];
            }
        }
        $donnees['images'] = new Images(Factory::fillArrayWithObjects($donnees['images'], 'image'));
        $donnees['artists'] = new Artists(Factory::fillArrayWithObjects($donnees['artists'], 'artist'));
        $donnees['extraartists'] = new Extraartists(Factory::fillArrayWithObjects($donnees['extraartists'], 'artist'));
        $donnees['tracklist'] = new Tracklist(Factory::fillArrayWithObjects($donnees['tracklist'], 'track'));
        $donnees['genres'] = new Genres(Factory::fillArrayWithObjects($donnees['genres'], 'genre'));
        $donnees['styles'] = new Styles(Factory::fillArrayWithObjects($donnees['styles'], 'style'));
        $this->hydrate($donnees);
    }

    public function __toArray() {
        return array($this->_id,
            $this->_images,
            $this->_artists,
            $this->_title,
            $this->_extraartists,
            $this->_formats,
            $this->_genres,
            $this->_styles,
            $this->_country,
            $this->_released,
            $this->_tracklist);
    }

    public function __toString() {
        return print_r($this->__toArray(), true);
    }

    private function hydrate(array $donnees) {
        foreach ($donnees as $key => $value) {
// On récupère le nom du setter correspondant à l'attribut.
            $method = 'set' . ucfirst($key);

// Si le setter correspondant existe.
            if (method_exists($this, $method)) {
// On appelle le setter.
                $this->$method($value);
            }
        }
    }

}

?>